﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.SYN;
using DaInterFace.SYN.E01;

namespace E01
{
     
    /// <summary>
    /// 同步销售账户
    /// </summary>
    public class A06 : ISYNChildSet
    {   
        public void Code(ISYN handler, CodeResult result, SYNRight user, SerialType st, String parameter)
        {
            List<E01A06E> E = new List<E01A06E>();
            E01A06P P = (E01A06P)handler.Serial.Deserialize(st, typeof(E01A06P), parameter);
            if (!P.ListAdjustPrice.Equals("[]"))
            {
                E = (List<E01A06E>)handler.Serial.Deserialize(st, typeof(List<E01A06E>), P.ListAdjustPrice);
            }

            try
            {
                SqlParameter[] sps = new SqlParameter[6 + E.Count * 4];
                sps[0] = new SqlParameter("@ID", SqlDbType.NVarChar, 50);
                sps[0].Value = P.ID;
                sps[1] = new SqlParameter("@Number", SqlDbType.NVarChar, 50);
                sps[1].Value = P.Number;
                sps[2] = new SqlParameter("@BuildDate", SqlDbType.DateTime);
                sps[2].Value = Convert.ToDateTime(P.BuildDate);
                sps[3] = new SqlParameter("@CusName", SqlDbType.NVarChar, 50);
                sps[3].Value = P.CusName;
                sps[4] = new SqlParameter("@Remark", SqlDbType.NVarChar, 50);
                sps[4].Value = P.Remark;
                sps[5] = new SqlParameter("@PassMode", SqlDbType.Int);
                sps[5].Value = P.PassMode;

                for (int i = 0; i < E.Count; i++)
                {
                    sps[6 + 4 * i] = new SqlParameter("@ProInfo" + i.ToString(), SqlDbType.NChar, 50);
                    sps[6 + 4 * i].Value = E[i].ProInfo;
                    sps[7 + 4 * i] = new SqlParameter("@ExecPolicy" + i.ToString(), SqlDbType.NChar, 50);
                    sps[7 + 4 * i].Value = E[i].ExecPolicy;
                    sps[8 + 4 * i] = new SqlParameter("@TaxPrice" + i.ToString(), SqlDbType.Decimal);
                    sps[8 + 4 * i].Value = E[i].TaxPrice;
                    sps[9 + 4 * i] = new SqlParameter("@Index" + i.ToString(), SqlDbType.Int);
                    sps[9 + 4 * i].Value = E[i].Index;
                }

                StringBuilder sql = new StringBuilder();
                sql.Append("SET NOCOUNT ON\r\n");
                sql.Append("DECLARE @Type AS  NVARCHAR(50) ");
                sql.Append("DECLARE @ErrorCount AS INT ");
                sql.Append("SET @ErrorCount=0 ");
                sql.Append("DECLARE @CusNameFID INT\r\n SET @CusNameFID = IsNull((SELECT FItemID FROM t_Organization WHERE FHomePage=@CusName AND FHomePage<>''),0)\r\n");
                sql.Append("DECLARE @FID INT\r\n SET @FID=IsNull((SELECT FID FROM KEW_SalesAccount WHERE FText1=@ID AND FText1<>''),0)");
                sql.Append("IF @FID<=0 BEGIN\r\n");
                //sql.Append("    \r\n");
                //sql.Append("END ELSE BEGIN\r\n");
                sql.Append("  BEGIN TRAN ");
                sql.Append("  EXEC GetICMaxNum 'KEW_SalesAccount',@FID OUTPUT\r\n");
                sql.Append("  INSERT INTO KEW_SalesAccount ([FClassTypeID],[FID],[FBillNo],[FDecimal],[FDecimal1],[FDecimal2],[FNote],[FBase],[FDate],[FBiller],[FBase1],[FBase3],[FBase4],[FBase5],[FText1]) ");
                sql.Append("  VALUES (236900147,@FID,@Number,0,0,0,@Remark,@CusNameFID,@BuildDate,0,0,0,0,0,@ID)\r\n");
                sql.Append("  SET @ErrorCount=@ErrorCount+@@ERROR ");
                sql.Append("  DECLARE @ProInfoFID INT\r\n");
                for (int i = 0; i < E.Count; i++)
                {
                    sql.Append("SET @ProInfoFID = IsNull((SELECT FID FROM KEW_ProductsEntry WHERE FText=@ProInfo" + i.ToString() + " AND FText<>''),0)\r\n");
                    sql.Append("IF (SELECT COUNT(1) FROM KEW_SalesAccountEntry WHERE FID =@FID AND FIndex=@Index" + i.ToString() + " ) > 0 BEGIN \r\n ");
                    sql.Append("  UPDATE KEW_SalesAccountEntry SET FBase2=@ProInfoFID,FIndex=@Index" + i.ToString() + ", FDecimal3=@TaxPrice" + i.ToString() + ", FText= @ExecPolicy" + i.ToString() + " WHERE FID =@FID AND FIndex=@Index" + i.ToString() + " \r\n");
                    sql.Append("END ELSE BEGIN\r\n");
                    sql.Append("INSERT INTO KEW_SalesAccountEntry ([FID],[FBase2],[FIndex],[FBase1],[FDecimal3],[FText]) VALUES (@FID,@ProInfoFID,@Index" + i.ToString() + ",0,@TaxPrice" + i.ToString() + ",@ExecPolicy" + i.ToString() + ")\r\n ");
                    sql.Append("END \r\n");
                    sql.Append("  SET @ErrorCount=@ErrorCount+@@ERROR ");
                    //sql.Append("SET @ProInfoFID = IsNull((SELECT FID FROM KEW_ProductsEntry WHERE FText=@ProInfo" + i.ToString() + "),0)\r\n");
                    //sql.Append("INSERT INTO KEW_SalesAccountEntry ([FID],[FBase2],[FIndex],[FBase1],[FDecimal3],[FText]) VALUES (@FID,@ProInfoFID,@Index" + i.ToString() + ",0,@TaxPrice" + i.ToString() + ",@ExecPolicy" + i.ToString() + ")\r\n ");
                }
                sql.Append("  SET @Type ='I'\r\n");

                sql.Append("IF @ErrorCount=0 AND (SELECT COUNT(1) FROM KEW_SalesAccount WHERE FBillNo=@Number)=1 ");
                sql.Append("BEGIN ");
                sql.Append("SELECT @Type,@FID\r\n ");
                sql.Append("COMMIT TRAN ");
                sql.Append("END ");
                sql.Append("ELSE BEGIN ");
                sql.Append("SELECT 'FALSE','事务出错回滚' ");
                sql.Append("ROLLBACK TRAN ");
                sql.Append("END ");

                sql.Append("END \r\n");
                sql.Append("SET NOCOUNT OFF\r\n");
                Recordset rs = handler.Data.ExeProcedure(P.BaseNo, sql.ToString(), sps);
              
                if (rs.Enable)
                {
                    if (rs.Data.Tables[0].Rows[0][0].ToString() == "U")
                    {
                        result.Result = EnumResult.Update;
                        result.Content = rs.Data.Tables[0].Rows[0][1].ToString();
                    }
                    else
                    {
                        result.Result = EnumResult.Insert;
                        result.Content = rs.Data.Tables[0].Rows[0][1].ToString();
                    }
                    result.Message = String.Format("销售账户同步完成:{0},{1}", P.Number, result.Result.ToString());
                }
                else
                {
                    result.Result = EnumResult.None;
                    result.Message = String.Format("销售账户同步失败:{0}", rs.Message);
                }
            }
            catch (Exception ex)
            {
                result.Result = EnumResult.None;
                result.Message = String.Format("销售账户同步失败:{0}", ex.Message);
            }
        }

    }

    /// <summary>
    /// 销售账户
    /// </summary>
    public class E01A06P
    {
        /// <summary>
        /// 账套编号
        /// </summary>
        public String BaseNo;
        /// <summary>
        /// 三方内码
        /// </summary>
        public String ID;
        /// <summary>
        /// 三方编号
        /// </summary>
        public String Number;
        /// <summary>
        /// 三方名称
        /// </summary>
        //public String Name;
        /// <summary>
        /// 下单日期
        /// </summary>
        public String BuildDate;
        /// <summary>
        /// 客户名称
        /// </summary>
        public String CusName;
        /// <summary>
        /// 备注
        /// </summary>
        public String Remark;
        /// <summary>
        /// 密码模式
        /// </summary>
        public int PassMode;
      
        /// <summary>
        /// 调价单
        /// </summary>
        public String ListAdjustPrice;

    }
    /// <summary>
    /// 销售账户-调价单
    /// </summary>
    public class E01A06E
    {

        /// <summary>
        /// 产品
        /// </summary>
        public String ProInfo;
        /// <summary>
        /// 执行政策
        /// </summary>
        public String ExecPolicy;
        /// <summary>
        /// 含税单价
        /// </summary>
        public decimal TaxPrice;
        /// <summary>
        /// 执行政策顺序号
        /// </summary>
        public int Index;
    }
    /// <summary>
    /// 字典—销售账户
    /// </summary>
    /*public class SalesAccount
    {
        /// <summary>
        /// 三方内码
        /// </summary>
        public String AccountISN;
        /// <summary>
        /// FID
        /// </summary>
        public String AccountFID;
        /// <summary>
        /// 账户编号
        /// </summary>
        public String AccountNo;

    }*/
}
